import { cityArea } from './mapCityArea'
import echarts from 'echarts'
// 或者地图数据
export function getMapData (params) {
  let bdary = new window.BMap.Boundary()
  let mapObj = {
    type: 'FeatureCollection',
    features: []
  }
  let mapData = []
  let PromiseArr = params.mapData.map(function (item, index) {
    return new Promise((resolve, reject) => {
      let nameData = ''
      let searchName = ''
      if (item.indexOf(',') > -1) {
        let arr = item.split(',')
        nameData = arr[arr.length - 1]
        searchName = item.replace(/,/g, '')
      } else {
        searchName = nameData = item
      }
      searchName = cityArea[searchName] ? cityArea[searchName] : searchName
      bdary.get(searchName, function (rs) {
        let data = rs.boundaries
        let dataStrArr = ''
        let arr = []
        mapData = []
        let mapDataObj = {
          type: 'Feature',
          properties: { name: item },
          geometry: { type: 'Polygon', coordinates: [] }
        }
        if (data.length === 0) {
          resolve('')
        }
        for (let m = 0; m < data.length; m++) {
          arr = []
          dataStrArr = data[m].split(';')
          switch (params.type) {
            case 'all': mapDataObj = {
              type: 'FeatureCollection',
              features: []
            }
              break
            case 'province': mapDataObj = {
              type: 'Feature',
              geometry: { type: 'Polygon', coordinates: [] },
              properties: { name: m === 0 ? nameData : nameData + ':' + m }
            }
              break
            case 'city': mapDataObj = {
              type: 'Feature',
              properties: { name: m === 0 ? nameData : nameData + ':' + m },
              geometry: { type: 'Polygon', coordinates: [] }
            }
              break
          }
          for (let i = 0; i < dataStrArr.length; i++) {
            arr.push([Number(dataStrArr[i].split(',')[0]), Number(dataStrArr[i].split(',')[1])])
          }
          mapDataObj.geometry.coordinates = [arr]
          if (data.length === 1) {
            resolve(mapDataObj)
          } else {
            mapData.push(mapDataObj)
          }
        }
        if (data.length > 1) {
          resolve(mapData)
        }
      })
    })
  })
  Promise.all(PromiseArr).then(res => {
    let data = res.filter(function (item) {
      return item
    })
    let isNotArea = false
    if (data.length === 1) {
      isNotArea = true
    }
    data = data.reduce((r, item) => r.concat(item), [])
    mapObj.features = [...data]
    echarts.registerMap(params.mapName, mapObj)
    bdary = null
    PromiseArr = null
    mapObj = null
    mapData = null
    params.callback(params.callbackData, data, isNotArea)
  }).catch(reason => {
    bdary = null
    PromiseArr = null
    mapObj = null
    mapData = null
    console.log('bbbbbb', reason)
  })
}
